<template>
  <LoginHeader>联合登录</LoginHeader>
  <section class="container">
    <nav class="tab">
      <a
        :class="{ active: hasAccount }"
        href="javascript:;"
        @click="hasAccount = true"
      >
        <i class="iconfont icon-bind" />
        <span>已有小兔鲜账号，请绑定手机</span>
      </a>
      <a
        :class="{ active: !hasAccount }"
        href="javascript:;"
        @click="hasAccount = false"
      >
        <i class="iconfont icon-edit" />
        <span>没有小兔鲜账号，请完善资料</span>
      </a>
    </nav>
    <div class="tab-content" v-if="hasAccount">
      <CallbackBind :unionId="unionId"></CallbackBind>
    </div>
    <div class="tab-content" v-else>
      <CallbackPatch :unionId="unionId"></CallbackPatch>
    </div>
  </section>
  <LoginFooter></LoginFooter>
</template>

<script>
import LoginHeader from './components/login-header.vue'
import LoginFooter from './components/login-footer.vue'
import CallbackBind from './components/callback-bind.vue'
import CallbackPatch from './components/callback-patch.vue'
import { ref } from 'vue'
import { userQQLogin } from '@/api/user'
import { Message } from '@/components'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
const QC = window.QC
export default {
  name: 'LoginCallback',
  components: {
    LoginHeader,
    LoginFooter,
    CallbackBind,
    CallbackPatch
  },
  setup() {
    const hasAccount = ref(true)
    const unionId = ref(null)
    const store = useStore()
    const router = useRouter()
    // 获取QQ登录的一些信息 比如  openId
    // console.log('qq是否登录', QC.Login.check())
    // 1. 判断qq是否登录
    if (QC.Login.check()) {
      // 2. 获取登录的qq的信息
      QC.Login.getMe(async openId => {
        unionId.value = openId
        // 3. 发送请求到自己的服务器，判断使用该openId能够登录成功
        try {
          const res = await userQQLogin(openId)
          // 4. 登录成功了
          Message({ text: '登录成功' })
          store.commit('user/setProfile', res.result)
          router.push('/')
        } catch {
          Message({ type: 'error', text: 'QQ未绑定' })
        }
      })
    }
    return {
      hasAccount,
      unionId
    }
  }
}
</script>

<style scoped lang="less">
.container {
  padding: 25px 0;
}
.tab {
  background: #fff;
  height: 80px;
  padding-top: 40px;
  font-size: 18px;
  text-align: center;
  a {
    color: #666;
    display: inline-block;
    width: 350px;
    line-height: 40px;
    border-bottom: 2px solid #e4e4e4;
    i {
      font-size: 22px;
      vertical-align: middle;
    }
    span {
      vertical-align: middle;
      margin-left: 4px;
    }
    &.active {
      color: @xtxColor;
      border-color: @xtxColor;
    }
  }
}
.tab-content {
  min-height: 600px;
  background: #fff;
}
</style>
